var queryAfterSales = require('./queryAfterSales')
var mealPromotion = require('./mealPromotion')
var sellerInfo = require('./sellerInfo')
var dealRecords = require('./dealRecords')
var fs = require('fs');
var http = require('http');
var util = require('util');
var readline = require('readline');
var log4js = require('log4js');
var stream = require('stream')
var tmallCombo = require('./tmallCombo')
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('logs/testTbskip.log'), 'testTbskip');
var logger = log4js.getLogger('testTbskip');
var workingCount = 0;
var monitorServer = http.createServer(function(req, res){
	res.end('read line: ' + lineNum + '  list size: ' + lineList.length + ' workingCount:' + workingCount);
});

var fileReadStream = fs.createReadStream('/home/looop/tbskip/queryTmallCombo.log');

var lineReader = readline.createInterface({
	input : fileReadStream,
	output : new stream.Writable()
});

fileReadStream.on('error', function(e){
	console.log(e)
})
fileReadStream.on('end', function(){
	console.log('fileReadStream end');
	lineReader.close();
})

var lineNum = 0;
var lineList = new Array();
lineReader.on('line', function(line){
	lineNum++;
	if(line.length > 1){
		if(line.indexOf('"') > 0){
			line = line.substring(0, line.indexOf('"'));
		}
		if(line.indexOf(queryAfterSales.tbskipApi) == 0){
			workingCount++;
			queryAfterSales.verify(line, function(){
				workingCount--;
				checkCount();
			});
//			lineList.push(line);
		}else if(line.indexOf(mealPromotion.tbskipApi) == 0){
			if(line.indexOf('meal=1') > 0 && (line.indexOf('jsonpInitExtension') > 0 || line.indexOf('TShop.mods.SKU.Promotion.getDefaultData') > 0)){
				workingCount++;
				mealPromotion.verify(line, function(){
					workingCount--;
					checkCount();
				});
//				lineList.push(line)
			}
		}else if(line.indexOf(sellerInfo.tbskipApi) == 0 && line.indexOf('&callback=jsonpSellerInfo&') > 0){
			workingCount++;
			sellerInfo.verify(line, function(){
				workingCount--;
				checkCount();
			});
//			lineList.push(line)
		}else if(line.indexOf('show_buyer_list.htm') > 0 && line.indexOf('&sbn=') > 0){
			workingCount++;
			dealRecords.verify(line, function(){
				workingCount--;
				checkCount();
			})
		}else if(tmallCombo.checkTmallComboUrl(line)){
			workingCount++;
			tmallCombo.verify(line, function(){
				workingCount--;
				checkCount();
			});
		} else {
			logger.error(line)
		}
	}
	checkCount();
}).on('close', function(){
	
	console.log('line number: ' + lineList.length)
	monitorServer.close();
//	nextLine();
	
}).on('error', function(e){
	logger.error('Error', e)
})


monitorServer.listen(10000, '127.0.0.1');

function nextLine(){
	if(lineList.length == 0){
		return;
	}
	var line = lineList.pop();
	if(line.indexOf(queryAfterSales.tbskipApi) == 0){
		queryAfterSales.verify(line);
	}else if(line.indexOf(mealPromotion.tbskipApi) == 0){
		if(line.indexOf('meal=1') > 0){
			mealPromotion.verify(line);
		}
	}else if(line.indexOf(sellerInfo.tbskipApi) == 0){
		sellerInfo.verify(line);
	}
	setTimeout(function(){
		nextLine()
	}, 100);
}

function checkCount(){
	if(workingCount > 10){
		lineReader.pause();
	}else{
		lineReader.resume();
	}
}